package com.tbsoft.ehr.dao.department;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tbsoft.ehr.dao.BaseDaoImpl;
import com.tbsoft.ehr.model.Department;

public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements
		DepartmentDao {

	public List<Department> getAllDepartments() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery("from Department d where d.status<>'D'");
				return q.list();
			}
		});
	}

	public Department getDepartmentById(String id) {
		return (Department) getHibernateTemplate().get(Department.class, id);
	}

	public List<Department> getDepartmentByName(final String name) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session
						.createQuery("from Department d where d.name=? ");
				q.setString(0, name);
				return q.list();
			}
		});
	}

}
